package org.taishan.electronshop.dao;

import org.taishan.electronshop.domain.Customer;
import org.taishan.electronshop.domain.Goods;
import org.taishan.electronshop.domain.Order;
import org.taishan.electronshop.util.DateHelper;
import org.taishan.electronshop.util.JdbcHelper;

import java.util.*;

public class OrderDao implements Dao<Order,String> {
    private OrderDao od;
    private CustomerDao cd=new CustomerDao();

    private static final String table = " t_orders ";


    @Override
    public Order load(String name) {
        String SQL = "SELECT id , name , address , telephone , create_time , total , customer_id,state, operate FROM" + table  + "WHERE name = ? " ;
        JdbcHelper helper = new JdbcHelper();
        List<Map<String,Object>> list = helper.query( SQL , name ) ;
        helper.destory();
        if( list != null && list.size() == 1 ) {
            Map<String,Object> map = list.get( 0 );
            Order o =   this.cast( map );
            return o ;
        }
        return null;
    }

    public Order load(String name,String oid) {
        String SQL = "SELECT id , name , address , telephone , create_time , total , customer_id,state, operate FROM" + table  + "WHERE name = ? and id = ?" ;
        JdbcHelper helper = new JdbcHelper();
        List<Map<String,Object>> list = helper.query( SQL , name ,oid) ;
        helper.destory();
        if( list != null && list.size() == 1 ) {
            Map<String,Object> map = list.get( 0 );
            Order o =   this.cast( map );
            return o ;
        }
        return null;
    }

    @Override
    public List list() {
        String SQL = "SELECT id , name , address , telephone , create_time , total , customer_id,state, operate FROM" + table   ;
        JdbcHelper helper = new JdbcHelper();
        List<Map<String,Object>> list = helper.query( SQL ) ;
        helper.destory();
        if( list != null && list.size() > 0  ) {
            List<Order> orders = new ArrayList<>();

            for( int i = 0 ; i < list.size() ; i++ ) {
                Map<String, Object> map = list.get(i);
                Order o =   this.cast( map );
                orders.add(o);
            }

            return orders ;
        }
        return null;
    }


    public List<Order> list(Integer userid) {
        String SQL = "SELECT id , name , address , telephone , create_time , total , customer_id,state, operate FROM"  + table + " WHERE customer_id = ? ";
        JdbcHelper helper = new JdbcHelper();
        List<Map<String, Object>> list = helper.query(SQL,userid);
        helper.destory();
        if (list != null && list.size() > 0) {
            List<Order> orders = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> map = list.get(i);
                Order o = this.cast(map);
                orders.add(o);
            }
            return orders;
        }
        return null;
    }

    public List<Order> list(Integer userid,String state) {
        String SQL = "SELECT id , name , address , telephone , create_time , total , customer_id,state, operate FROM"  + table + " WHERE customer_id = ? and state = ?";
        JdbcHelper helper = new JdbcHelper();
        List<Map<String, Object>> list = helper.query(SQL,userid,state);
        helper.destory();
        if (list != null && list.size() > 0) {
            List<Order> orders = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> map = list.get(i);
                Order o = this.cast(map);
                orders.add(o);
            }
            return orders;
        }
        return null;
    }

    @Override
    public boolean save(Order o) {
        String SQL = "INSERT INTO " + table +
                " ( id , name , address,telephone , create_time, total,customer_id,state, operate) " +
                " VALUES (  ? , ? , ? , ? ,? , ? , ? ,? ,?)";
        java.sql.Date ct = DateHelper.cast(o.getCreateTime());
        String oid = o.getId();
        String name = o.getName();
        String address = o.getAddress();
        String phone = o.getTelephone();
        Double tol =  o.getTotal();
        Integer cid = o.getCustomer().getId();
        String state = o.getState();
        String operate = o.getOperate();
        Object[] array = { oid , name ,address ,phone , ct,tol,cid,state,operate} ;

        JdbcHelper helper = new JdbcHelper();
        // 执行插入语句，并获得由数据库产生的主键值
        int id = helper.insert(SQL, array);
        helper.destory();
        // 如果 id 不等于 -1  则视为保存成功( 在 JdbcHelper 的 insert 方法中有约定 )
        return id != -1;
    }

    @Override
    public boolean update(Order o) {
        String SQL = "UPDATE " + table +
                " SET state = ? , operate = ?" +
                " WHERE id = ? ";
        String state = o.getState();
        String operate = o.getOperate();
        String oid = o.getId();
//        if (number <= 0) {
//            td.delete(id);
//        }
        Object[] params = {state,operate, oid};

        JdbcHelper helper = new JdbcHelper();
        int count = helper.update(SQL, params);
        helper.destory();
        return count == 1;
    }

    @Override
    public boolean delete(String id) {
        return false;
    }

    @Override
    public Order cast(Map<String,Object> map) {
        Order o = new Order();
        o.setId( (String) map.get( "id" ) );
        o.setName( (String)map.get( "name" ) );
        o.setAddress( (String) map.get( "address" ) );
        o.setTelephone( (String) map.get( "telephone" ) );
        o.setCreateTime( (Date) map.get( "create_time" ) );
        o.setTotal( (Double) map.get( "total" ) );
        o.setState((String) map.get( "state" ));
        o.setOperate((String) map.get( "operate" ));
        System.out.println("xx"+(Integer) map.get( "customer_id" ));
        Customer c = cd.load((Integer) map.get( "customer_id" ));
        System.out.println(c);
        o.setCustomer(c);
        return o;
    }
}
